<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusId">
              <j-dict-select-tag type="list" v-model="model.cusId"  dictCode="mws_t_arc_customers,code,id" placeholder="请选择户号" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <a-input v-model="model.cusName" placeholder="请输入户名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readMonth">
              <a-input v-model="model.readMonth" placeholder="请输入抄表月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="起度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginScale">
              <a-input-number v-model="model.beginScale" placeholder="请输入起度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="止度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endScale">
              <a-input-number v-model="model.endScale" placeholder="请输入止度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="合计金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalMoney">
              <a-input-number v-model="model.totalMoney" placeholder="请输入合计金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="垃圾费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lajifee">
              <a-input-number v-model="model.lajifee" placeholder="请输入垃圾费" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="收费方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feeChargeType">
              <j-dict-select-tag type="list" v-model="model.feeChargeType"  dictCode="fee_charge_type" placeholder="请选择收费方式" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="前次余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="preBalance">
              <a-input-number v-model="model.preBalance" placeholder="请输入前次余额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="预存缴费金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storedPaymentMoney">
              <a-input-number v-model="model.storedPaymentMoney" placeholder="请输入预存缴费金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="违约金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="penalty">
              <a-input-number v-model="model.penalty" placeholder="请输入违约金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="费用金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入费用金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="实收金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recivedMoney">
              <a-input-number v-model="model.recivedMoney" placeholder="请输入实收金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="本次余额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postBalance">
              <a-input-number v-model="model.postBalance" placeholder="请输入本次余额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readerId">
              <j-select-user-by-dep v-model="model.readerId" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bookId">
              <j-dict-select-tag type="list" v-model="model.bookId"  dictCode="" placeholder="请选择抄表本" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="冲账状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feeStruckStatus">
              <j-dict-select-tag type="list" v-model="model.feeStruckStatus"  dictCode="fee_struck_status" placeholder="请选择冲账状态" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="财务月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="financialMonth">
              <a-input v-model="model.financialMonth" placeholder="请输入财务月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status"  dictCode="" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="收费明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="chgSubFeesTable.loading"
            :columns="chgSubFeesTable.columns"
            :dataSource="chgSubFeesTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ChgFeesForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        validatorRules: {
           cusId: [
              { required: true, message: '请输入户号!'},
           ],
           readMonth: [
              { required: true, message: '请输入抄表月份!'},
           ],
           amount: [
              { required: true, message: '请输入水量!'},
           ],
           feeChargeType: [
              { required: true, message: '请输入收费方式!'},
           ],
           preBalance: [
              { required: true, message: '请输入前次余额!'},
           ],
           storedPaymentMoney: [
              { required: true, message: '请输入预存缴费金额!'},
           ],
           money: [
              { required: true, message: '请输入费用金额!'},
           ],
           recivedMoney: [
              { required: true, message: '请输入实收金额!'},
           ],
           postBalance: [
              { required: true, message: '请输入本次余额!'},
           ],
           readerId: [
              { required: true, message: '请输入抄表员!'},
           ],
           bookId: [
              { required: true, message: '请输入抄表本!'},
           ],
           feeStruckStatus: [
              { required: true, message: '请输入冲账状态!'},
           ],
           financialMonth: [
              { required: true, message: '请输入财务月份!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['chgSubFees', ],
        tableKeys:['chgSubFees', ],
        activeKey: 'chgSubFees',
        // 收费明细
        chgSubFeesTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '费用代码',
              key: 'itemCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '用水性质',
              key: 'natureId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '阶梯号',
              key: 'tierNo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '单价',
              key: 'price',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '水量',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '收费金额',
              key: 'money',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '增值税金额',
              key: 'vatMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/chg/chgFees/add",
          edit: "/chg/chgFees/edit",
          chgSubFees: {
            list: '/chg/chgFees/queryChgSubFeesByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
     addBefore(){
            this.chgSubFeesTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.chgSubFees.list, params, this.chgSubFeesTable)
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          chgSubFeesList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>